package cn.web.tunnelrobot.controller;

import cn.web.tunnelrobot.StartService;
import cn.web.tunnelrobot.dao.ProjectDao;
import cn.web.tunnelrobot.dao.UserRecordDao;
import cn.web.tunnelrobot.dto.HardwareTestInfoDTO;
import cn.web.tunnelrobot.dto.ImageInfoDTO;
import cn.web.tunnelrobot.dto.ReportListDTO;
import cn.web.tunnelrobot.dto.TunnelDistanceDTO;
import cn.web.tunnelrobot.pojo.Project;
import cn.web.tunnelrobot.pojo.User;
import cn.web.tunnelrobot.pojo.UserRecord;
import cn.web.tunnelrobot.service.HardwareTestInfoService;
import cn.web.tunnelrobot.service.ImageInfoService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.MileageConversion;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 报表查看列表信息
 * @version 1.0.0
 *
 */
@RestController
@RequestMapping("/hardwareTestInfo")
@Api(tags = "沉降收敛相关接口")
@ApiSort(value = 5)
public class HardwareTestInfoController {
    @Autowired
    private HardwareTestInfoService hardwareTestInfoService;
    @Autowired
    private ImageInfoService imageInfoService;
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private UserRecordDao userRecordDao;
    //获取用户session信息
    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    //User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    //log4j
    Logger logger = LoggerFactory.getLogger(HardwareTestInfoController.class);
    MileageConversion mileageConversion = new MileageConversion();//米数转换里程
    @GetMapping("/getTunnelLengthById")
    @ResponseBody
    public HashMap<String, Object> getTunnelLengthById(@RequestParam Integer prId) {
        HashMap<String, Object> map = new HashMap<>();
        //查询用户所属项目ID
        //WorkArea workArea = workAreaDao.getWorkAreaByUsId(usId);
        Project project = projectDao.getProjectById(prId);
        if(project == null){
            map.put("code",0);
            map.put("data","");
        }
       logger.info("隧道长度:"+project.getPrTunnelLength());
       logger.info("隧道长度:"+project.getPrTunnelLength()/1000);
       int km = 0;
       Float kilometer = project.getPrTunnelLength() /1000;
       logger.info("隧道长度:"+kilometer);
        //根据索道长度划分一级 KM
        if(kilometer <= 1){ //划分一千米
            km = 1;
        }else if(kilometer > 1 && kilometer <= 2 ){ //划分二千米
            km = 2;
        } else if(kilometer > 2 && kilometer <= 3 ){ //划分三千米
            km = 3;
        } else if(kilometer > 3 && kilometer <= 4 ){ //划分四千米
            km = 4;
        } else if(kilometer > 4 && kilometer <= 5 ){ //划分五千米
            km = 5;
        } else if(kilometer > 5 && kilometer <= 6 ){ //划分六千米
            km = 6;
        } else if(kilometer > 6 && kilometer <= 7 ){ //划分七千米
            km = 7;
        } else if(kilometer > 7 && kilometer <= 8 ){ //划分八千米
            km = 8;
        } else if(kilometer > 8 && kilometer <= 9 ){ //划分九千米
            km = 9;
        } else if(kilometer > 9 && kilometer <= 10 ){ //划分十千米
            km = 10;
        }else  if(kilometer > 10){//划分十千米
            km = 10;
        }
       logger.info("treeview划分:"+km);
        TunnelDistanceDTO tunnelDistanceDTO = new TunnelDistanceDTO();
        tunnelDistanceDTO.setTitle("请选择隧道距离");
        tunnelDistanceDTO.setKey("0");
        tunnelDistanceDTO.setMileage("DK0+0");
        List<TunnelDistanceDTO> tunnelDistanceDTOList = new ArrayList<>();
        for (int i=0;i<km;i++){//渲染二级菜单
            TunnelDistanceDTO tunnelDistance1 = new TunnelDistanceDTO();
            tunnelDistance1.setTitle((i+1)+"km");
            tunnelDistance1.setKey(String.valueOf(((i+1)*1000)));
            tunnelDistance1.setMileage(mileageConversion.mileage((i+1)*1000));
            tunnelDistanceDTOList.add(tunnelDistance1);
            //渲染二级
            List<TunnelDistanceDTO> tunnelDistanceDTOList2 = new ArrayList<>();
            for(int j=0;j<1000;j=j+10){
                TunnelDistanceDTO tunnelDistance2 = new TunnelDistanceDTO();
                if(i*1000+j == 0 || i*1000+j == 1000 || i*1000+j == 2000 || i*1000+j == 3000 || i*1000+j == 4000 || j ==5000 || i*1000+j == 6000 || i*1000+j == 7000 || i*1000+j == 8000 || i*1000+j == 9000
                        ||i*1000+j== 10000){
                   logger.info("不符合规则");
                }else{
                    tunnelDistance2.setTitle(i*1000+j+"m");
                    tunnelDistance2.setKey(String.valueOf((i*1000+j)));
                    tunnelDistance2.setMileage(mileageConversion.mileage(i*1000+j));
                }
                //0-1000米
                tunnelDistanceDTOList2.add(tunnelDistance2);
                //System.out.println("tunnelDistance2的长度:"+tunnelDistanceDTOList2.size());
            }
            tunnelDistanceDTOList2.remove(0);
            tunnelDistance1.setChildren(tunnelDistanceDTOList2);
        }
        tunnelDistanceDTO.setChildren(tunnelDistanceDTOList);
        //最外层一级
        List<TunnelDistanceDTO> tunnelDistanceDTOLists = new ArrayList<>();
        tunnelDistanceDTOLists.add(tunnelDistanceDTO);
        map.put("tunnelDistance",tunnelDistanceDTOLists);
        //map.put("prId",workArea.getPrId());
        return map;
    }
    @GetMapping("/getHardwareTestInfoByRange")
    @ResponseBody
    @ApiOperation(value = "项目ID，pageNo，pageSize",notes = "获取报表信息")
    public HashMap<String, Object> getHardwareTestInfoByRange(Integer prId,
                                                           Integer beginHtMileage,
                                                           Integer endHtMileage,
                                                              @ApiParam("页号")@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                              @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        //判断里程是不是数字型
        return hardwareTestInfoService.getHardwareTestInfoByRange(prId,beginHtMileage,endHtMileage,pageNo,pageSize);
    }
    @GetMapping("/getHardwareTestInfoData")
    @ResponseBody
    @ApiOperation(value = "项目ID，pageNo，pageSize",notes = "获取报表信息")
    public HashMap<String, Object> getHardwareTestInfoData(Integer prId,
                                                           String htTurnMeter1,
                                                           @ApiParam("页号")@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                           @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return hardwareTestInfoService.getHardwareTestInfoByPrIdAndMileage(prId,htTurnMeter1,pageNo,pageSize);
    }

    @GetMapping("/getImageInfoByPrIdAndMileage")
    @ResponseBody
    public HashMap<String, Object> getImageInfoByPrIdAndMileage(@RequestParam Integer prId,@RequestParam Integer imMileage) {
        return imageInfoService.getImageInfoByPrIdAndMileage(prId,imMileage);
    }

    @GetMapping("/getReportList")
    @ResponseBody
    public HashMap<String, Object> getReportList(@RequestParam Integer imMileage) {
       HashMap<String,Object> map = new HashMap<>();
       List<ReportListDTO> list = new ArrayList<>();
       for (int i=0;i<5;i++){
           ReportListDTO reportListDTO = new ReportListDTO();
           reportListDTO.setKey(String.valueOf((i+1)));
           if(i==0){
               //reportListDTO.setName("隧道"+ MileageConversion.mileage(imMileage) +"米处中轴线检测");
               reportListDTO.setName("隧道中轴线检测");//整个隧道只有一条中轴线
           }else  if(i==1){
               reportListDTO.setName("隧道轮廓线检测");
           }else if(i==2){
               reportListDTO.setName("隧道"+ MileageConversion.mileage(imMileage) +"处当前沉降-收敛值");
           }else if(i==3){
               reportListDTO.setName("隧道"+ MileageConversion.mileage(imMileage) +"处累计沉降-收敛值");
           }else if(i==4){
               reportListDTO.setName("隧道"+ MileageConversion.mileage(imMileage) +"处隧道检测报告");
           }
           list.add(reportListDTO);
        }
       map.put("code",0);
       map.put("data",list);
       /* UserRecord userRecord = new UserRecord();
        userRecord.setUsId(user.getUsId());
        userRecord.setUrOperatContent("进入报表查看页面");
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
       return map;
    }

    @GetMapping("/addHardwareTestInfoDTO")
    @ResponseBody
    @ApiOperation(value = "激光模块，htAngle，collectWay",notes = "添加报表信息")
    public HashMap<String, Object> addHardwareTestInfoDTO(@RequestParam Integer whichLaserModule,
                                                          @RequestParam Integer htAngle,Integer collectWay){
        return hardwareTestInfoService.addHardwareTestInfoDTO(whichLaserModule,htAngle,collectWay);
    }
    @GetMapping("/testHardware")
    @ResponseBody
    @ApiOperation(value = "激光模块测试 测试使用",notes = "仅测试使用")
    public HashMap<String, Object> testHardware(@RequestParam Integer whichLaserModule,
                                                          @RequestParam Integer htAngle,Integer collectWay){
        return hardwareTestInfoService.testHardware(whichLaserModule,htAngle,collectWay);
    }
    @GetMapping("/getLaserModuleNumber")
    @ResponseBody
    @ApiOperation(value = "无",notes = "获取激光模块number")
    public HashMap<String, Object> getLaserModuleNumber(){
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for(int i = 1;i<=StartService.projectDTO.getPrLaserModuleNumber();i++){
            list.add(i);
        }
        map.put("code",0);
        map.put("data",list);
        return map;
    }
    @GetMapping("/getImageInfoList")
    @ResponseBody
    @ApiOperation(value = "查询列表信息")
    public HashMap<String, Object> getImageInfoList(@RequestParam (required = false)Integer prId,
                                                    @RequestParam (required = false) Integer imMileage,
                                                    @RequestParam(required = false) Integer isId,
                                                    @RequestParam(required = false) Integer imBelongsPlate,
                                                    @RequestParam(required = false) Integer imConstructionStatus,
                                                    @RequestParam(required = false) Integer imDecideStatus,
                                                    @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                    @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return imageInfoService.getImageInfoList(prId,imMileage,isId,imBelongsPlate,imConstructionStatus,imDecideStatus,pageNo,pageSize);
    }
    @GetMapping("/getImageInfoListNew")
    @ResponseBody
    @ApiOperation(value = "查询列表信息")
    public HashMap<String, Object> getImageInfoListNew(@RequestParam (required = false)Integer imId,
                                                       @RequestParam (required = false)String partsConstruction,
                                                       @RequestParam (required = false)String masterItems,
                                                       @RequestParam (required = false)String beginMileage,
                                                       @RequestParam (required = false)String endMileage

                                                       ){
        ImageInfoDTO imageInfoDTO1=new ImageInfoDTO();
        imageInfoDTO1.setImId(imId);
        imageInfoDTO1.setPartsConstruction(partsConstruction);
        imageInfoDTO1.setMasterItems(masterItems);
        if(beginMileage==null&&endMileage==null){

        }else {
            //判断里程是不是数字型
            boolean flag = MileageConversion.isNumeric(beginMileage);
            if (flag == false) {
                int dist = MileageConversion.distance(beginMileage);
                String dists = String.valueOf(dist);
                imageInfoDTO1.setBeginMileage(dists);
            }
            //判断里程是不是数字型
            boolean flag1 = MileageConversion.isNumeric(endMileage);
            if (flag1 == false) {
                int dist = MileageConversion.distance(endMileage);
                String dists = String.valueOf(dist);
                imageInfoDTO1.setEndMileage(dists);
            }
        }

        return imageInfoService.getImageInfoListNew(imageInfoDTO1);
    }
    @GetMapping("/getImageInfoListByRange")
    @ResponseBody
    @ApiOperation(value = "查询列表信息")
    public HashMap<String, Object> getImageInfoListByRange(@RequestParam (required = false)Integer imId,
                                                       @RequestParam (required = false)String partsConstruction,
                                                       @RequestParam (required = false)String masterItems,
                                                       @RequestParam (required = false)String beginMileage,
                                                       @RequestParam (required = false)String endMileage,
                                                       @ApiParam("页号")@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                       @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        ImageInfoDTO imageInfoDTO1=new ImageInfoDTO();
        imageInfoDTO1.setImId(imId);
        imageInfoDTO1.setPartsConstruction(partsConstruction);
        imageInfoDTO1.setMasterItems(masterItems);
        imageInfoDTO1.setBeginMileage(beginMileage);
        imageInfoDTO1.setEndMileage(endMileage);
        return imageInfoService.getImageInfoListFY(imageInfoDTO1,pageNo,pageSize);
    }
}
